package com.example.util.page;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.publice.generator.TemplateUtil;
import com.example.util.DateUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SeachParamsUtil {

    /**
     * 是否加载 查询条件
     *
     * @param cnd
     * @param k
     * @param v
     * @return
     */
    private static boolean idLoadCnd(String cnd, String k, Object v) {
        return k.startsWith(cnd) && null != v && v.toString().length() > 0;
    }

    /**
     * 加载 搜索条件
     *
     * @param pager
     * @param wrapper
     */
    public static void loadSearchParam(Pager pager, QueryWrapper<?> wrapper) {
        if (pager != null && pager.getSearchParams() != null && pager.getSearchParams().size() > 0) {
            Map<String, Object> searchParams = pager.getSearchParams();
            if (null != searchParams) {
                searchParams.forEach((k, v) -> {
                    if (idLoadCnd(SearchParam.SEARCH_EQ, k, v)) {
                        wrapper.eq(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_EQ)[1]), v);
                    } else if (idLoadCnd(SearchParam.SEARCH_GE, k, v)) {
                        wrapper.ge(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_GE)[1]), String.valueOf(v));
                    } else if (idLoadCnd(SearchParam.SEARCH_LE, k, v)) {
                        wrapper.le(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LE)[1]), String.valueOf(v));
                    } else if (idLoadCnd(SearchParam.SEARCH_LIKE, k, v)) {
                        wrapper.like(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_LIKE)[1]), String.valueOf(v));
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_GE, k, v)) {//时间大于小于
                        wrapper.ge(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_GE)[1]), String.format("%s%s", v, " 00:00:00"));
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_LE, k, v)) {//时间大于小于
                        wrapper.le(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_LE)[1]), String.format("%s%s", v, " 23:59:59"));
                    } else if (idLoadCnd(SearchParam.SEARCH_IN, k, v)) {//包含
                        wrapper.in(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_IN)[1]), v);
                    } else if (idLoadCnd(SearchParam.SEARCH_DT_BETWEEN, k, v)) {//时间区间
                        List dts = (ArrayList) v;
                        dts.add(0, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(0).toString())));
                        dts.add(1, DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, DateUtils.dateTime(DateUtils.YYYY_MM_DD, dts.get(1).toString())));
                        wrapper.ge(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]), String.format("%s%s", dts.get(0), " 00:00:00"));
                        wrapper.le(TemplateUtil.camelTounderline(k.split(SearchParam.SEARCH_DT_BETWEEN)[1]), String.format("%s%s", dts.get(1), " 23:59:59"));
                    }
                });
            }
        }


    }


}
